package com.edu.d2_stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo2 {
    /**
     * stream流常用的API（中间操作）,下面都是stream的API,可以使用stream.APi调用
     */
    /**
     * 1、filter对流中的数据进行过滤
     * 2、count统计流中数据个数
     * 3、limit获取前几个数据
     * 4、skip跳过前几个元素
     * 5、distinct去除流中的重复元素，依赖（hashCode和equals方法）
     * 6、concat合并两个流为一个流
     * 7、map加工
     */
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌","张三","赵敏","张三丰","力气","赵敏","张三丰");

/*        list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        });*/
/*list.stream().filter(new Predicate<String>() {
    @Override
    public boolean test(String s) {
        return s.startsWith("张");
    }
}).collect(Collectors.toList());*/
        Stream<String> stream = list.stream().filter(s -> s.startsWith("张") && s.length() == 3);
        System.out.println(stream.collect(Collectors.toList()));

        long count = list.stream().filter(s -> s.startsWith("张")).count();
        System.out.println(count);

        List<String> list1 = list.stream().limit(2).collect(Collectors.toList());
        System.out.println(list1);
        list.stream().filter(s -> s.startsWith("张")).limit(2).forEach(s -> System.out.println(s));

        List<String> list2 = list.stream().skip(2).collect(Collectors.toList());
        System.out.println(list2+"--------------------------");
        list.stream().filter(s -> s.startsWith("张")).skip(1).forEach(s -> System.out.println(s));

        //map加工方法：第一个参数：原材料；第二个参数：加工后的材料
        //例如：给所有的数据前，都加上一个“黑马的”
/*        list.stream().map(new Function<String, String>() {
            @Override
            public String apply(String s) {
                return "黑马的"+s;
            }
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });*/
         list.stream().map(s -> "黑马的"+s).forEach(s -> System.out.println(s));
         //转换成list集合
        System.out.println(list.stream().map(s -> "黑马的" + s).collect(Collectors.toList()));

        //把所有的名称都加工成一个对象
/*        Studentlist.stream().map(new Function<Student, Student>() {
            @Override
            public Student apply(Student student) {
                return student;
            }
        }).forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });*/
        Stream<Student> stu=list.stream().map(s -> new Student(s));
        System.out.println(stu.collect(Collectors.toList()));
        //concat（）合并两个流
        //定义一个数组流先，用于等下合并
        System.out.println("------------------------");
        Stream<String> s1 = Stream.of("学习", "坚持");
        Stream<String> s2 = list.stream().filter(s -> s.startsWith("张"));
        Stream<String> concat = Stream.concat(s1, s2);
        //foreach（）方法为终结方法
       // concat.forEach(s -> System.out.println(s));
        //distinct去重复
        concat.distinct().forEach(s -> System.out.println(s));
    }
}
